<%--
  Created by IntelliJ IDEA.
  User: MAX
  Date: 2021-01-21
  Time: 23:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>物资基础信息管理</title>
    <base href="<%=request.getContextPath()%>/"/>
    <link rel="stylesheet" href="webjars/layui/2.5.6/css/layui.css">
</head>
<body>

<fieldset class="layui-elem-field" style="margin-left:75px;margin-right:75px ">
    <div class="layui-field-box">
        <form class="layui-form" lay-filter="search" style="margin: 0 auto">
            <div class="layui-form-item layui-inline">
                <label class="layui-form-label">物资名称:</label>
                <div class="layui-input-inline">
                    <input type="text" name="cgName" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item layui-inline">
                <label class="layui-form-label">物资编码:</label>
                <div class="layui-input-inline">
                    <input type="text" name="cgCode" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item layui-inline">
                <label class="layui-form-label">助记码:</label>
                <div class="layui-input-inline">
                    <input type="text" name="inputCode" class="layui-input">
                </div>
            </div>

            <div class=" layui-form-item layui-inline">
                <button type="button" onclick="search();" class="layui-btn layui-btn-normal"><i class="layui-icon layui-icon-search"></i></button>
            </div>

            <div class=" layui-form-item layui-inline">
                <button type="button" onclick="creat();" class="layui-btn"> <i class="layui-icon">&#xe654;</i></button>
            </div>

        </form>


    </div>
</fieldset>
 
<div class="layui-container">

    <table id="dictionaryTb" lay-filter="dictionaryTb"></table>

    <script type="text/html" id="action">
        <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i></a>
    </script>
</div>



<%-- 添加--%>
<div style="display: none;"  id="create">
    <form class="layui-form" lay-filter="create">
        <input type="reset" style="display: none" id="restAdd" />
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">物资名称:</label>
            <div class="layui-input-inline">
                <input type="text" name="cgName" class="layui-input"  required  lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">物资编码:</label>
            <div class="layui-input-inline">
                <input type="text" name="cgCode" class="layui-input"  required  lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">助记码:</label>
            <div class="layui-input-inline">
                <input type="text" name="inputCode" class="layui-input" required  lay-verify="required" maxlength="6">
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">最小规格:</label>
            <div class="layui-input-inline">
                <input type="text" name="spec" class="layui-input"  required  lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">最小单位:</label>
            <div class="layui-input-inline">
                <input type="text" name="unit" class="layui-input"  required  lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">包装单位:</label>
            <div class="layui-input-inline">
                <input type="text" name="packUnit" class="layui-input"  required  lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">包装规格:</label>
            <div class="layui-input-inline">
                <input type="text" name="packSpec" class="layui-input"  required  lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">生产厂家:</label>
            <div class="layui-input-inline">
                <input type="text" name="factory" class="layui-input"  required  lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">单价:</label>
            <div class="layui-input-inline">
                <input type="text" name="unitprice" class="layui-input"  required  lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">物资类别:</label>
            <div class="layui-input-inline">
                <select name="type" lay-filter="type" lay-verify="">
                    <option value="1">高值耗材</option>
                    <option value="0">低值耗材</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">是否禁用:</label>
            <div class="layui-input-inline">
                <select name="deleteFlag" lay-filter="deleteFlag" lay-verify="">
                    <option value="Y">启用</option>
                    <option value="N">禁用</option>
                </select>
            </div>
        </div>


    </form>
</div>



<%-- 添加--%>
<div style="display: none;"  id="update">
    <form class="layui-form" lay-filter="update">

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">物资名称:</label>
            <div class="layui-input-inline">
                <input type="text" name="cgName" class="layui-input"  required  lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">物资编码:</label>
            <div class="layui-input-inline">
                <input type="text" name="cgCode" class="layui-input"  required  lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">助记码:</label>
            <div class="layui-input-inline">
                <input type="text" name="inputCode" class="layui-input" required  lay-verify="required" maxlength="6">
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">最小规格:</label>
            <div class="layui-input-inline">
                <input type="text" name="spec" class="layui-input"  required  lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">最小单位:</label>
            <div class="layui-input-inline">
                <input type="text" name="unit" class="layui-input"  required  lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">包装单位:</label>
            <div class="layui-input-inline">
                <input type="text" name="packUnit" class="layui-input"  required  lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">包装规格:</label>
            <div class="layui-input-inline">
                <input type="text" name="packSpec" class="layui-input"  required  lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">生产厂家:</label>
            <div class="layui-input-inline">
                <input type="text" name="factory" class="layui-input"  required  lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">单价:</label>
            <div class="layui-input-inline">
                <input type="text" name="unitprice" class="layui-input"  required  lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">物资类别:</label>
            <div class="layui-input-inline">
                <select name="type" lay-filter="type" lay-verify="">
                    <option value="1">高值耗材</option>
                    <option value="0">低值耗材</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">是否禁用:</label>
            <div class="layui-input-inline">
                <select name="deleteFlag" lay-filter="deleteFlag" lay-verify="">
                    <option value="Y">启用</option>
                    <option value="N">禁用</option>
                </select>
            </div>
        </div>


    </form>
</div>



<script src="webjars/layui/2.5.6/layui.js"></script>
<script>
    layui.use(["table","form","jquery"],function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.$;
        let params = layui.form.val("search");
        table.render({
            id    : "dictionaryTb",
            elem  : "#dictionaryTb",
            where : params,
            url   : "dictionary/getDictionary",
            cols  : [[
                {title: "物资编码", field: "cgCode",width:100},
                {title: "物资名称", field: "cgName",width:160},
                {title: "最小规格", field: "spec",width:80},
                {title: "最小单位", field: "unit",width:80},
                {title: "包装单位", field: "packUnit",width:80},
                {title: "包装规格", field: "packSpec",width:80},
                {title: "单价", field: "unitprice",width:100,edit:"text"},
                {title: "生产厂家", field: "factory",width:240},
                {title: "操作", toolbar: "#action"}
            ]],
            page: true
        })
        table.on("tool(dictionaryTb)",function ({data,event}) {
            switch (event) {
                case "edit" :
                    update(data);
                    break;
                case "del" :
                    del(data);
                    break;
            }
        });
        table.on('edit(dictionaryTb)', function(obj){
            console.log(obj.value); //得到修改后的值
            console.log(obj.field); //当前编辑的字段名
            console.log(obj.data); //所在行的所有相关数据
        });
    })

    //条件模糊查询
    function search() {
        layui.use(["table", "form", "jquery"], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.$;
            let params = layui.form.val("search");
            //console.log(params);
            layui.table.reload('dictionaryTb', {
                where: params
            });

        });
    }



    function creat() {
        layui.use(["layer", "jquery", "form", "table"], function () {
            let layer = layui.layer;
            let $ = layui.$;
            let table = layui.table;
            layer.open({
                type: 1,
                area: ['700px', '440px'],
                skin: 'layui-layer-lan',
                title: '注册信息',
                content: $("#create"),
                btn: ["创建", "返回"],
                yes() {

                    let cgName = $('#create input[name=cgName]').val();
                    if (cgName == null || cgName == "") {
                        layer.msg("请输入物资名称！");
                        return;
                    }

                    let cgCode = $('#create input[name=cgCode]').val();
                    if (cgCode == null || cgCode == "") {
                        layer.msg("请输入物资编码！");
                        return;
                    }

                    let inputCode = $('#create input[name=inputCode]').val();
                    if (inputCode == null || inputCode == "") {
                        layer.msg("请输入助记码！");
                        return;
                    }

                    let spec = $('#create input[name=spec]').val();
                    if (spec == null || spec == "") {
                        layer.msg("请输入最小规格！");
                        return;
                    }

                    let unit = $('#create input[name=unit]').val();
                    if (unit == null || unit == "") {
                        layer.msg("请输入最小单位！");
                        return;
                    }

                    let packUnit = $('#create input[name=packUnit]').val();
                    if (packUnit == null || packUnit == "") {
                        layer.msg("请输入包装单位！");
                        return;
                    }

                    let packSpec = $('#create input[name=packSpec]').val();
                    if (packSpec == null || packSpec == "") {
                        layer.msg("请输入包装规格！");
                        return;
                    }

                    let unitprice = $('#create input[name=unitprice]').val();
                    if (unitprice == null || unitprice == "") {
                        layer.msg("请输入单价！");
                        return;
                    }
                    if (isNaN(unitprice) || parseFloat(unitprice) <= 0 ) {
                        layer.msg("请输入正确单价！");
                        return;
                    }

                    let params = layui.form.val("create");
                    params.unitprice = parseFloat(unitprice);
                    console.log(JSON.stringify(params));

                    $.post({
                        url: "dictionary/createDictionary",
                        data: JSON.stringify(params),
                        contentType: 'application/json'
                    })
                        .done(({msg}) => {
                            layui.table.reload("dictionaryTb");
                            layer.close(layer.index);
                            layer.msg(msg);
                        });
                    $("#restAdd").click();
                }
            });
        });
    }




    function update(data) {

        layui.use(["layer","jquery","form","table"],function () {
            let layer = layui.layer;
            let $ = layui.$;
            let table = layui.table;
            //console.log(data);
            layui.form.val("update",data);
            layer.open({
                type : 1,
                content : $("#update"),
                btn : ["确定","取消"],
                yes(){
                    let params = layui.form.val("update");
                    params.dictionaryId = data.dictionaryId;
                    //console.log(JSON.stringify(params));

                    $.post({
                        url:"dictionary/updateDictionary",
                        data:JSON.stringify(params),
                        contentType:'application/json'})
                        .done(({msg})=>{
                            layui.table.reload("dictionaryTb");
                            layer.close(layer.index);
                            layer.msg(msg);
                        });
                }
            });
        })
    }



    function del(data) {

        layui.use(["layer","jquery","form","table"],function () {
            let layer = layui.layer;
            let $ = layui.$;
            let table = layui.table;
            //console.log(data);
            layer.confirm('您确定要删除这条数据吗？', {
                btn: ['确定','取消'] //按钮
            }, function(){

                $.post({
                    url: "dictionary/deleteDictionary",
                    data: {
                        'dictionaryId':data.dictionaryId
                    },
                    success : function(result){
                        layui.table.reload("dictionaryTb");
                        //layer.close(layer.index);
                        layer.msg(result.msg);
                    }
                });
            });


        })
    }
</script>
</body>
</html>
